package com.helei.consumerListener;

import com.alibaba.fastjson.JSON;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 *
 * @date 2024/10/2 16:55
 */
//同步发送消息注解
@Component

//key值过滤
@RocketMQMessageListener(topic ="deliver-topic-boot", consumerGroup = "group-deliver-boot")
public class ConsumerDeliverListenerRocketMq implements RocketMQListener<MessageExt> {

    //MessageExt类型---是消息的所有内容，没有报错就签收，如果报错就拒收。
    @Override
    public void onMessage(MessageExt messageExt) {
        System.out.println("收到消息了---end"+new Date());
        System.out.println("标签类型---->"+JSON.toJSONString(messageExt.getTags()));
        try {
            System.out.println("接收到了 消息:" + new String(messageExt.getBody()));
        }catch (Exception e){
            e.printStackTrace();

        }

        //如果存在key值则打印key值
        if (messageExt.getKeys() != null){
            System.out.println("key值为："+messageExt.getKeys());
        }


    }

}
